﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace SaveFuel.NumberSelector
{
    public partial class TotalDistance : PhoneApplicationPage
    {
        public TotalDistance()
        {
            InitializeComponent();
            this.Cell1.DataSource = new NumberSelector.NumbersDataSource() { Minimum = 0, Maximum = 2, SelectedItem = 0 };
            LoadDataBack();
        }

        private void LoadDataBack()
        {
            if (App.distance != string.Empty)
            {
                int[] distanceDigit = new int[6];
                string totalDistance = App.distance;

                if (App.distance.Length < distanceDigit.Length )
                {
                    totalDistance = "0" + totalDistance;
                }

                for (int i = 0; i < totalDistance.Length; i++)
                {
                    distanceDigit[i] = Convert.ToInt32(totalDistance[i].ToString());
                }

                WriteDataToLoopingSelector(distanceDigit);
            }
        }

        private void WriteDataToLoopingSelector(int[] distanceDigit)
        {
            this.Cell1.DataSource.SelectedItem = distanceDigit[0];
            this.Cell2.DataSource.SelectedItem = distanceDigit[1];
            this.Cell3.DataSource.SelectedItem = distanceDigit[2];
            this.Cell4.DataSource.SelectedItem = distanceDigit[3];
            this.Cell5.DataSource.SelectedItem = distanceDigit[4];
            this.Cell6.DataSource.SelectedItem = distanceDigit[5];
        }

        private void doneClick_Click(object sender, EventArgs e)
        {
            string distance = this.Cell1.DataSource.SelectedItem.ToString() + this.Cell2.DataSource.SelectedItem.ToString() + this.Cell3.DataSource.SelectedItem.ToString() + this.Cell4.DataSource.SelectedItem.ToString() + this.Cell5.DataSource.SelectedItem.ToString() + this.Cell6.DataSource.SelectedItem.ToString();
            App.distance = distance;

            NavigationService.GoBack();
        }

        private void cancelClick_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}